package com.pansoft.openplanet.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.baseadapter.listview.CommonAdapter;
import com.efounder.baseadapter.listview.ViewHolder;
import com.pansoft.openplanet.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 备份助记词
 * Created by will on 18-3-20.
 */

public class BackupTokenHelperActivity extends TCBaseActivity implements View.OnClickListener {

    private GridView gridView;
    private String number;
    private String userName;
    private String mnemonic;
    private String address;
    private String ethPublicKey;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.talk_chain_activity_backup_token_helper);
        setEnableScreenShotListen(false);
        number = getIntent().getExtras().getString("number");
        userName = getIntent().getExtras().getString("userName");
        mnemonic = getIntent().getExtras().getString("mnemonic");
        address = getIntent().getExtras().getString("address");
        ethPublicKey = getIntent().getExtras().getString("ethPublicKey");

        ImageView ivBack = (ImageView) findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        List<String> mnemonics = new ArrayList<>();
        String[] arr = mnemonic.split("\\s+");
        for (String ss : arr) {
            mnemonics.add(ss);
        }
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(R.string.openplanet_title_back_mnemonic);
        TextView tvAddress = (TextView) findViewById(R.id.tv_address);
        tvAddress.setText(address);
        Button tvNext = (Button) findViewById(R.id.tv_next);
        tvNext.setOnClickListener(this);
        gridView = (GridView) findViewById(R.id.gv_token_helper);
        gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        gridView.setAdapter(new MyTokenHelperAdapter(this, R.layout.talk_chain_item_token_helper, mnemonics));

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.tv_next) {
//            new AlertDialog.Builder(BackupTokenHelperActivity.this)
//                    .setTitle(R.string.common_text_hint)
//                    .setMessage("助记词可用于恢复钱包账户或重置钱包账户密码，是否确认已经记下助记词")
//                    .setPositiveButton("下一步", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialogInterface, int i) {

            //如果从我的钱包界面过来，跳回原界面
//                            if (getIntent().hasExtra(CreateWalletActivity.LAST_ACTIVITY)) {
//                                String lastActivity = getIntent().getStringExtra(CreateWalletActivity.LAST_ACTIVITY);
//                                if ("myWallet".equals(lastActivity)) {
//                                    Intent intent1 = new Intent(BackupTokenHelperActivity.this, MyWalletActivity.class);
//                                    ActivityCompat.finishAffinity(BackupTokenHelperActivity.this);
//                                    return;
//                                }
//
//                            }

//                            if (EnvironmentVariable.getProperty("old_user").equals("1")) {
            Intent intent1 = new Intent(BackupTokenHelperActivity.this, TokenCheckActivity.class);
            intent1.putExtra("mnemonic", mnemonic);
            intent1.putExtra("userName", userName);
            intent1.putExtra("number", number);
            intent1.putExtra("ethPublicKey", ethPublicKey);
            intent1.putExtra("address", address);
            intent1.putExtra("loginPwd",  getIntent().getStringExtra("loginPwd"));

            intent1.putExtra("ethPrivateKey", getIntent().getStringExtra("ethPrivateKey"));
            BackupTokenHelperActivity.this.startActivity(intent1);
//                                ActivityCompat.finishAffinity(BackupTokenHelperActivity.this);
//                            } else {
//                                Intent intent = new Intent(BackupTokenHelperActivity.this, SetupAvatarActivity.class);
//                                intent.putExtra("userName", userName);
//                                intent.putExtra("number", number);
//                                startActivity(intent);
//                            }
//                        }
//                    })
//                    .setNegativeButton("再看看", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialogInterface, int i) {
//
//                        }
//                    })
//                    .create()
//                    .show();

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public class MyTokenHelperAdapter extends CommonAdapter<String> {
        public MyTokenHelperAdapter(Context context, int layoutId, List<String> datas) {
            super(context, layoutId, datas);
        }

        @Override
        protected void convert(ViewHolder viewHolder, String item, int position) {
            viewHolder.setText(R.id.tv_token_helper, position + 1 + "." + item);
        }
    }
}
